package com.small.site.service.impl;

import com.small.site.base.ILogService;
import com.small.site.enums.ResponseCode;
import com.small.site.exception.BusinessException;
import com.small.site.ibase.IBaseService;
import com.small.site.pojo.SysLog;
import com.small.site.service.LogService;
import com.small.site.util.R;
import com.small.site.vo.PageVo;
import com.small.site.vo.req.LogPageReqVo;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiaochi
 * @date 2021/12/15 16:18
 * @desc
 */
@Service
@SuppressWarnings("all")
public class LogServiceImpl implements LogService {

    @DubboReference(check = false)
    private ILogService logService;

    /**
     * 新增日志
     * @param sysLog
     * @return
     */
    public R<String> insert(SysLog sysLog){
        int result = logService.insert(sysLog);
        if (result == 0){
            throw new BusinessException(ResponseCode.ERROR);
        }
        return R.ok(ResponseCode.SUCCESS.getMessage());
    }

    /**
     * 分页查找操作日志
     * @param pageNum
     * @param pageSize
     * @param where
     * @return
     */
    @Override
    public PageVo<SysLog> findPage(LogPageReqVo logPageReqVo) {
        IBaseService.Criteria<SysLog> criteria = logService.createCriteria(SysLog.class);
        criteria.andGreaterThanOrEqualTo(SysLog::getCreateTime,StringUtils.isNotBlank(logPageReqVo.getStartTime()),logPageReqVo.getStartTime());
        criteria.andLessThanOrEqualTo(SysLog::getCreateTime,StringUtils.isNotBlank(logPageReqVo.getEndTime()),logPageReqVo.getEndTime());
        criteria.orderBy(SysLog::getCreateTime).desc();
        return logService.findPage(logPageReqVo.getPageNum(),logPageReqVo.getPageSize(),criteria);
    }

    /**
     * 批量删除日志
     * @param logIds
     * @return
     */
    @Override
    public R<String> deletedLog(List<String> logIds) {
        int result = logService.batchDeletedLog(logIds);
        if (result == 0) {
            return R.error();
        }
        return R.ok();
    }
}
